import path from 'path'
import fs from 'fs-extra'

export async function findFilePath(
  configPathDir: string,
  fileName: string,
  deep = 4,
): Promise<string | undefined> {
  if (deep < 0) return;
  const configPath = path.resolve(configPathDir, fileName)
  if (await fs.pathExists(configPath)) {
    return configPath
  } else {
    const nextDir = path.dirname(configPathDir)
    if (nextDir === configPathDir) return undefined
    return findFilePath(nextDir, fileName, deep - 1)
  }
}

export async function findFirstExistFilePath(filePath: string[]) {
  for (const f of filePath) {
    const p = await fs.pathExists(f)
    if (p) return f
  }
  return null;
}